/*
  ******************
  ******************

  注意：url用的端口为 82端口

  ******************
  ******************
*/


let $messageInfo = $(".message-info");
let $nameInp = $(".form #name");
let $textarea = $(".form #textarea");
let $send = $(".form .btn button");
let sendData = { username: "", content: "" };

getList();

$send.on("click", () => {
  sendData.username = $nameInp.val();
  sendData.content = $textarea.val();
  if ($nameInp.val() === "") {
    sendData.username = "匿名有户"
  }
  $.ajax({
    url: "http://localhost:82/app.php",
    type: "post",
    data: sendData,
    success(res) {
      if (res == 200) {
        getList()
      }
    },
    error(err) {
      console.log(res.status)
    }
  })
});


function getList() {
  $.ajax({
    url: "http://localhost:82/app.php",
    type: "get",
    success(res) {
      $messageInfo.html("");
      let datas = JSON.parse(res)[0];
      console.log(datas)
      if (datas.code === 200) {
        $((datas.data).reverse()).each((idx, item) => {
          let $odiv = $(`
              <div class="list">
                <div class="name">${item.name}</div>
                <div class="content">${item.content}</div>
              </div>
            `);
          $messageInfo.append($odiv)
        })
      }
    },
    error(err) {
      console.log(err.status)
    }
  })
}